package com.rockcent.mall.lib.domain;

import com.rockcent.common.domain.IDEntityMall;
import com.rockcent.mall.lib.domain.enumclass.UploadFileBusinessType;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Calendar;
import java.util.UUID;

@Entity
@Table(name = "upload_image_file")
@DynamicInsert
@DynamicUpdate
public class UploadImageFile extends IDEntityMall {

    @Column(columnDefinition = "varchar(255) comment'文件名'")
    private String fileName;
    @Column(columnDefinition = "varchar(255) comment'源文件文件名'")
    private String originFileName;
    @Column(columnDefinition = "int comment '文件大小'")
    private Long size;
    @Column(columnDefinition = "varchar(255) comment '文件路径 (可用于记录的唯一标识)'", unique = true)
    private String path;
    @Column(columnDefinition = "varchar(50) comment '图片使用的业务类型 PRODUCT_FACE(\"/product/face\",\"产品封面图\")'")
    @Enumerated(EnumType.STRING)
    private UploadFileBusinessType businessType;
    @Column(columnDefinition = "bigint comment '用户 id'")
    private Long userInfoId;
    @Column(columnDefinition = "bigint comment '商家 id'")
    private Long merchantId;
    @Column(columnDefinition = "bigint comment '平台 id'")
    private Long mallId;

    @Transient
    private Calendar calendar = Calendar.getInstance();


    /**
     * 生产上传文件名
     * @param originalFilename
     * @return
     */
    public String genFileName(String originalFilename) {
        StringBuffer fileName = new StringBuffer();
        fileName.append(calendar.get(Calendar.DAY_OF_MONTH))
                .append(calendar.get(Calendar.HOUR_OF_DAY))
                .append(calendar.get(Calendar.MINUTE))
                .append(calendar.get(Calendar.SECOND))
                .append(calendar.get(Calendar.MILLISECOND))
                .append(UUID.randomUUID().toString())
                .append(this.getExtFileName(originalFilename));
        return fileName.toString();
    }

    private String getExtFileName(String originFileName) {
        return originFileName.substring(originFileName.lastIndexOf("."));
    }

    /**
     * 生成文件的全路径
     * @param env
     * @param businessType
     * @param fileName
     * @return
     */
    public String genAllPath(String env, UploadFileBusinessType businessType, String fileName) {
        StringBuffer path = new StringBuffer(env);
        path.append(businessType.getPath())
                .append("/")
                .append(calendar.get(Calendar.YEAR))
                .append(calendar.get(Calendar.MONTH) + 1)
                .append("/")
                .append(fileName);
        return path.toString();
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getOriginFileName() {
        return originFileName;
    }

    public void setOriginFileName(String originFileName) {
        this.originFileName = originFileName;
    }

    public UploadFileBusinessType getBusinessType() {
        return businessType;
    }

    public void setBusinessType(UploadFileBusinessType businessType) {
        this.businessType = businessType;
    }

    public Long getUserInfoId() {
        return userInfoId;
    }

    public void setUserInfoId(Long userInfoId) {
        this.userInfoId = userInfoId;
    }

    public Long getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(Long merchantId) {
        this.merchantId = merchantId;
    }

    public Long getMallId() {
        return mallId;
    }

    public void setMallId(Long mallId) {
        this.mallId = mallId;
    }


    @Override
    public String toString() {
        return "UploadImageFile{" +
                "fileName='" + fileName + '\'' +
                ", originFileName='" + originFileName + '\'' +
                ", size=" + size +
                ", path='" + path + '\'' +
                ", businessType=" + businessType +
                ", userInfoId=" + userInfoId +
                ", merchantId=" + merchantId +
                ", mallId=" + mallId +
                ", calendar=" + calendar +
                '}';
    }
}